package com.zb.mobile.network.controller;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.annotation.SystemLog;
import com.zb.constant.LogConstant;
import com.zb.constant.OperationLogModuleType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.RemarkVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjNetwork;
import com.zb.exception.SMSException;
import com.zb.mobile.NetworkEmpMobileVo;
import com.zb.mobile.NetworkMobileCreateVo;
import com.zb.mobile.network.service.ConNetworkMobileService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.*;
import com.zb.service.SmsVerificCodeService;
import com.zb.utils.TokenNetworkUtil;
import com.zb.utils.VerificUtil;
import com.zb.vo.NetworkAloneTeamChangeVo;
import com.zb.vo.NetworkServiceAreaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/11/21 0021
 **/
@Api(tags = "服务商移动端类接口")
@RestController
@RequestMapping("/mobile/network")
public class NetworkMobileController {
    @Resource
    private ZjNetworkService networkService;
    @Resource
    private ConNetworkMobileService conNetworkMobileService;
    @Resource
    private ZjNetworkEmpRelationService networkEmpRelationService;
    @Resource
    private ZjNetworkAloneTeamChangeService networkAloneTeamChangeService;
    @Resource
    private ZjNetworkServiceAreaService networkServiceAreaService;
    @Resource
    private ZjNetworkStrategyService networkStrategyService;

    @ApiOperation("获取服务商团队模式")
    @GetMapping("/getNetworkTeamType")
    public Result getNetworkTeamType(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        ZjNetwork network = networkService.getById(loginInfo.getNetworkId());
        return Result.success().data("team", network.getTeamType() == 2);
    }

    @ApiOperation("生成邀请码")
    @GetMapping("/getInviteCode")
    public Result getInviteCode(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        ZjNetwork network = networkService.getById(loginInfo.getNetworkId());
        Assert.isTrue(network.getTeamType() == 1, "当前服务商团队模式为个人，不允许增加人员，如需增加人员，请升级服务商为团队模式");
        String inviteCode = SmsVerificCodeService.generatorJoinNetworkInviteCode(loginInfo.getCustAccId(), loginInfo.getNetworkId(), null);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("engineerInviteCode", inviteCode);
        return Result.success().data(PageUtil.row(resultMap));
    }

    @ApiOperation("发送短信邀请码")
    @GetMapping("/sendInviteCode")
    public Result sendInviteCode(HttpServletRequest request, @RequestParam @ApiParam(value = "接收邀请码的手机号", required = true) String phone) throws SMSException {
        Assert.isTrue(VerificUtil.phoneVerific(phone), "手机号格式错误");
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        ZjNetwork network = networkService.getById(loginInfo.getNetworkId());
        Assert.isTrue(network.getTeamType() == 1, "当前服务商团队模式为个人，不允许增加人员，如需增加人员，请升级服务商为团队模式");
        conNetworkMobileService.checkPhoneJoinNetwork(loginInfo, phone);
        return Result.success("邀请码发送成功");
    }

    @ApiOperation("校验工程师加入服务商的邀请码")
    @GetMapping("/checkJoinNetworkInviteCode/{inviteCode}")
    public Result checkJoinNetworkInviteCode(HttpServletRequest request, @PathVariable @ApiParam(value = "加入服务商的邀请码", required = true) String inviteCode) throws SMSException {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        networkService.checkJoinNetworkInviteCode(loginInfo, inviteCode);
        return Result.success();
    }

    @ApiOperation("根据邀请码获取服务商信息")
    @GetMapping("/getNetworkInfo/{inviteCode}")
    public Result getNetworkInfo(HttpServletRequest request, @PathVariable @ApiParam(value = "加入服务商的邀请码", required = true) String inviteCode) throws SMSException {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.row(networkService.getNetworkInfoByInviteCode(loginInfo, inviteCode)));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "加入服务商")
    @ApiOperation("加入服务商")
    @PostMapping("/joinNetwork/{inviteCode}")
    public Result joinNewtwork(HttpServletRequest request, @PathVariable @ApiParam(value = "加入服务商的邀请码", required = true) String inviteCode,
                               @RequestBody @Valid NetworkEmpMobileVo empMobileVo) throws SMSException {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        networkEmpRelationService.joinNewtwork(loginInfo, inviteCode, empMobileVo);
        return Result.success("加入服务商成功");
    }

    @ApiOperation("校验服务商加入企业的邀请码")
    @GetMapping("/checkNetworkJoinCustomer/{inviteCode}")
    public Result checkNetworkJoinCustomer(HttpServletRequest request, @PathVariable @ApiParam(value = "加入企业的邀请码", required = true) String inviteCode) throws SMSException {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        conNetworkMobileService.checkNetworkJoinCustomer(loginInfo, inviteCode);
        return Result.success();
    }

    @ApiOperation("根据邀请码获取企业信息")
    @GetMapping("/getCustomerInfo/{inviteCode}")
    public Result getCustomerInfo(HttpServletRequest request, @PathVariable @ApiParam(value = "加入企业的邀请码", required = true) String inviteCode) throws SMSException {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(conNetworkMobileService.getCustomerInfo(loginInfo, inviteCode));
    }

    @SystemLog(moduleType = OperationLogModuleType.NETWORK_TYPE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_3, remark = "创建服务商")
    @ApiOperation("创建服务商")
    @PostMapping("/saveNetwork/{inviteCode}")
    public Result saveNetwork(HttpServletRequest request, @PathVariable @ApiParam(value = "加入企业的邀请码", required = true) String inviteCode,
                              @RequestBody @Valid NetworkMobileCreateVo networkMobileCreateVo) throws SMSException {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        conNetworkMobileService.saveNetwork(loginInfo, inviteCode, networkMobileCreateVo);
        return Result.success("创建服务商成功");
    }

    @ApiOperation("获取服务商移动端人员列表")
    @PostMapping("/getNetworkMobileEmpList")
    public Result getNetworkMobileEmpList(HttpServletRequest request, @RequestBody SelectVo selectVo) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(networkEmpRelationService.getNetworkMobileEmpList(loginInfo, selectVo));
    }

    @ApiOperation("申请成为团队服务商")
    @PostMapping("/applyChangeToNetworkTeam")
    public Result applyChangeToNetworkTeam(HttpServletRequest request, @RequestBody @Valid NetworkAloneTeamChangeVo aloneTeamChangeVo) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        networkAloneTeamChangeService.applyChangeToNetworkTeam(loginInfo, aloneTeamChangeVo);
        return Result.success("申请提交成功，等待审核");
    }

    @ApiOperation("申请成为个人服务商")
    @PostMapping("/applyChangeToNetworkAlone")
    public Result applyChangeToNetworkAlone(HttpServletRequest request, @RequestBody @Valid NetworkAloneTeamChangeVo aloneTeamChangeVo) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        networkAloneTeamChangeService.applyChangeToNetworkAlone(loginInfo, aloneTeamChangeVo);
        return Result.success("申请提交成功，等待审核");
    }

    @ApiOperation("取消变更个人服务商/服务商团队申请")
    @PostMapping("/cancelChangeToNetworkApply/{id}")
    public Result cancelChangeToNetworkApply(HttpServletRequest request, @PathVariable @ApiParam(value = "变更申请记录ID", required = true) Long id,
                                             @RequestBody @Valid RemarkVo remarkVo) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        networkAloneTeamChangeService.cancelChangeToNetworkApply(loginInfo, id, remarkVo);
        return Result.success("变更申请取消成功");
    }

    @ApiOperation("获取服务商的服务区域")
    @GetMapping("/getNetworkArea")
    public Result getNetworkArea(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.rows(networkServiceAreaService.getNetworkArea(loginInfo.getNetworkId())));
    }

    @ApiOperation("保存服务商选择服务区域")
    @PostMapping("/saveNetworkArea")
    public Result saveNetworkArea(HttpServletRequest request, @RequestBody List<NetworkServiceAreaVo> areaVos) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        networkServiceAreaService.save(loginInfo.getCustAccId(), loginInfo.getNetworkId(), areaVos);
        return Result.success();
    }

    @ApiOperation("获取提现权限")
    @GetMapping("/getWithdrawRoot")
    public Result getWithdrawRoot(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.row(conNetworkMobileService.getWithdrawRoot(loginInfo)));
    }

    @ApiOperation("获取查询费用权限")
    @GetMapping("/getSelectFeeRoot")
    public Result getSelectFeeRoot(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.row(conNetworkMobileService.getSelectFeeRoot(loginInfo)));
    }

    @ApiOperation("获取手续费策略")
    @GetMapping("/getNetworkWithdrawHandlingFeeStrategy")
    public Result getNetworkWithdrawHandlingFeeStrategy(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.row(networkStrategyService.getNetworkWithdrawHandlingFeeStrategy(loginInfo.getNetworkId())));
    }

    @ApiOperation("获取是否允许超管代工程师提现")
    @GetMapping("/getIsAllowManagerHelpWithdrawStrategy")
    public Result getIsAllowManagerHelpWithdrawStrategy(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenNetworkUtil.getAccountLoginInfo(request);
        return Result.success().data(PageUtil.row(conNetworkMobileService.getIsAllowManagerHelpWithdrawStrategy(loginInfo)));
    }

}
